﻿Imports Mystery

Public Class SelectionFilter
    Implements IPropertyFilterControl

    Private _property_info As System.Reflection.PropertyInfo
    Private _content_property As ContentProperty
    Private _dictattr As StringPropertyDictAttr
    Private _dict As IStringPropertyDict

    Private _map As New Dictionary(Of ComboBoxItem, String)

    Public Property property_info As System.Reflection.PropertyInfo Implements IPropertyFilterControl.content_property
        Get
            Return _property_info
        End Get
        Set(value As System.Reflection.PropertyInfo)
            If value Is _property_info Then Exit Property
            _content_property = value.getMysteryAttribute(Of ContentProperty)()
            _dictattr = value.getMysteryAttribute(Of StringPropertyDictAttr)()
            If _dictattr Is Nothing Then
                _dict = New BaseStringSelection
            Else
                _dict = _dictattr.getDictionary(Nothing)
            End If

            label = _content_property.label
            _property_info = value

            For Each v As String In _dict.getValues()
                Dim cbi As New ComboBoxItem
                cbValue.Items.Add(cbi)
                cbi.Content = _dict.getLabelFor(v)
                _map(cbi) = v
            Next

        End Set
    End Property


    Public Property label As String Implements IPropertyFilterControl.label

    Public Event haveChanged() Implements IPropertyFilterControl.haveChanged

    Private Sub cbValue_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles cbValue.SelectionChanged
        RaiseEvent haveChanged()
    End Sub

    Public Function Match(content As Mystery.IContent) As Boolean Implements IPropertyFilterControl.Match
        If cbiAll.IsSelected Then Return True

        Dim content_value As Object = _content_property.retrive(content)
        If TypeOf (content_value) Is ContentPropertyValue Then content_value = DirectCast(content_value, ContentPropertyValue).getValue
        Return _map(cbValue.SelectedItem) = content_value
    End Function
End Class
